package br.com.empresa.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;

import br.com.empresa.dao.ProfessorDAO;
import br.com.empresa.modelo.Professor;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CadastroProfessorGUI extends JFrame {

	private JPanel contentPane;
	private JTextField txtCodigo;
	private JTextField txtNome;
	private JTextField txtTelefone;
	private JTextField txtCPF;
	private DefaultTableModel modelo;
	private int linhaSelecionada;

	public CadastroProfessorGUI(int cdProfessor, DefaultTableModel modelo,
			int linhaSelecionada) {
		this();
		this.modelo = modelo;
		this.linhaSelecionada = linhaSelecionada;
		ProfessorDAO dao = new ProfessorDAO();
		Professor f = dao.listarPorId(cdProfessor);
		
		txtCodigo.setText(String.valueOf(f.getCodigo()));
		txtNome.setText(f.getNome());
		
		
		txtTelefone.setText(f.getTelefone());
		txtCPF.setText(f.getCpf());
	}

	/**
	 * Create the frame.
	 */
	public CadastroProfessorGUI() {
		setBackground(new Color(255, 255, 255));
		setTitle("Sistema Academico");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 746, 448);
		contentPane = new JPanel();
		contentPane.setForeground(Color.WHITE);
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.CYAN);
		panel.setToolTipText("");
		panel.setBounds(0, 0, 730, 39);
		contentPane.add(panel);
		
		JLabel lblNewLabel = new JLabel("Professor");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblNewLabel.setForeground(Color.WHITE);
		panel.add(lblNewLabel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.CYAN);
		panel_1.setBounds(0, 387, 730, 21);
		contentPane.add(panel_1);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setVerticalAlignment(SwingConstants.BOTTOM);
		lblNewLabel_1.setForeground(Color.WHITE);
		panel_1.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("C\u00F3digo:");
		lblNewLabel_2.setBounds(10, 50, 46, 14);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Nome:");
		lblNewLabel_3.setBounds(10, 91, 75, 14);
		contentPane.add(lblNewLabel_3);
		
		txtCodigo = new JTextField();
		txtCodigo.setEditable(false);
		txtCodigo.setBounds(65, 50, 86, 20);
		contentPane.add(txtCodigo);
		txtCodigo.setColumns(10);
		
		txtNome = new JTextField();
		txtNome.setBounds(63, 88, 224, 20);
		contentPane.add(txtNome);
		txtNome.setColumns(10);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Professor f = new Professor();
				f.setNome(txtNome.getText());
				f.setCpf(txtCPF.getText());
				
				
				f.setTelefone(txtTelefone.getText());
				
				ProfessorDAO dao = new ProfessorDAO();
				if (!(txtCodigo.getText().trim().equals("") ||
						  txtCodigo.getText() == null)){
						f.setCodigo(Integer.parseInt(txtCodigo.getText()));
						dao.editar(f);
						
						modelo.removeRow(linhaSelecionada);
						modelo.addRow(new Object[] {f.getCodigo(), f.getTelefone()});
					}else{
						dao.inserir(f);
					}
					dispose();
				
					
			}
		});
		btnSalvar.setBounds(532, 353, 89, 23);
		contentPane.add(btnSalvar);
		
		JButton btnLimpar = new JButton("Limpar");
		btnLimpar.setBounds(631, 353, 89, 23);
		contentPane.add(btnLimpar);
		
		JLabel lblNewLabel_6 = new JLabel("Telefone:");
		lblNewLabel_6.setBounds(10, 183, 46, 14);
		contentPane.add(lblNewLabel_6);
		
		txtTelefone = new JTextField();
		txtTelefone.setBounds(66, 180, 143, 20);
		contentPane.add(txtTelefone);
		txtTelefone.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("CPF:");
		lblNewLabel_7.setBounds(10, 135, 46, 14);
		contentPane.add(lblNewLabel_7);
		
		txtCPF = new JTextField();
		txtCPF.setBounds(66, 132, 224, 20);
		contentPane.add(txtCPF);
		txtCPF.setColumns(10);
	}
}
